package com.atguigu.eduservice.controller.front;

import com.atguigu.commonutils.R;
import com.atguigu.commonutils.utils.LoginUser;
import com.atguigu.commonutils.utils.LoginUserUtil;
import com.atguigu.eduservice.client.SmsClient;
import com.atguigu.eduservice.client.UserClient;
import com.atguigu.eduservice.entity.UcenterMember;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author alan
 */
@RestController
@RequestMapping("/eduService/user")
@CrossOrigin
public class UserController {

    @Resource
    private SmsClient smsClient;
    @Resource
    private UserClient userClient;

    @ApiOperation("/发送注册验证码")
    @PostMapping("/code/register/{phone}")
    public R sendRegisterCode(@PathVariable("phone") String phone) {
        return smsClient.sendRegisterCode(phone);
    }

    @ApiOperation("/发送登录验证")
    @PostMapping("/code/login/{phone}")
    public R sendLoginCode(@PathVariable("phone") String phone) {
        return smsClient.sendLoginCode(phone);
    }

    @ApiOperation("注册会员")
    @PostMapping("/userservice/ucenter-member/register")
    public R register(@RequestBody UcenterMember ucenterMember) {
        return userClient.register(ucenterMember);
    }

    @ApiOperation("使用账号密码登录")
    @PostMapping("/userservice/ucenter-member/login")
    public R login(@RequestBody UcenterMember ucenterMember) {
        return userClient.login(ucenterMember);
    }

    @ApiOperation("退出登录")
    @PostMapping("/userservice/ucenter-member/logout")
    public R logout() {
        return userClient.logout();
    }

    @ApiOperation("从token中获取信息")
    @PostMapping("/userservice/ucenter-member/getInfo")
    public R getInfo() {
        LoginUser loginUser = LoginUserUtil.getLoginUser();
        if (ObjectUtils.isNotNull(loginUser)) {
            return R.ok().data("loginUser", loginUser);
        } else {
            return R.error("登录超时");
        }
    }

    @ApiOperation("更新会员信息")
    @PostMapping("/userservice/ucenter-member/updateInfo")
    public R updateInfo(@RequestBody UcenterMember ucenterMember) {
        return userClient.updateInfo(ucenterMember);
    }

    @ApiOperation("通过验证码登录")
    @PostMapping("/userservice/ucenter-member/loginByPhone/{phone}/{code}")
    public R loginByPhone(@PathVariable("phone") String phone, @PathVariable("code") String code) {
        return userClient.loginByPhone(phone, code);
    }

    @ApiOperation("/微信登陆")
    @GetMapping("/api/ucenter/wx/login")
    public String getCode() {
        return userClient.getCode();
    }

}
